
#include<windows.h>
#include<winnt.h>
#include<basetsd.h>
#include<gdiplus/gdiplus.h>
#include<ole2.h>
#include<errhandlingapi.h>
#include"globalVar.h"
#include"log"
extern const void* _binary_res_ic_menu_png_start;
extern const int _binary_res_ic_menu_png_size;
extern const void* _binary_res_ic_menu_png_end;
GdiplusStartupInput gsi;
HWND actionMenuWindow;
HWND gui;
int borderColor=0xff7f7f7f;
int borderWidth=1;
int itemCount;
int itemHeight;
int itemTextX;
int itemOnHover=-1;
int itemOnHoverColor=0x00afafaf;
const char *MENU_WINDOW_CLASS="CustomMenuWindowClass";
typedef struct BaseTab{
	char kind;
	union data{
		struct FileBrowserTab{
				char* path;
		};
	};
} BaseTab;

#include"WndProc.c"
#include"MenuProc.c"
#include"alertErr"
HANDLE out;
HANDLE in;
int WINAPI WinMain(HINSTANCE h1,HINSTANCE h2,LPSTR lpstr,int num){
	SetUnhandledExceptionFilter(unhandledExceptionFilter);
	HWND console=GetConsoleWindow();
	if(console){
		out=GetStdHandle(STD_OUTPUT_HANDLE);
		in=GetStdHandle(STD_INPUT_HANDLE);
		if(out&&out!=INVALID_HANDLE_VALUE){
			char str[256];
			SYSTEMTIME st;
			GetLocalTime(&st);
			int len=wsprintf(str,"%04d/%02d/%02d/%02d:%02d:%02d\n",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
			WriteConsoleA(out,str,len,NULL,NULL);
		}
	}
	HCURSOR idc_arrow=LoadCursor(NULL,IDC_ARROW);
	ic_logo=LoadIcon(h1,"MAIN");
	itemCount=sizeof(items)/sizeof(items[0]);
	itemHeight=40;
	WNDCLASSEX wc={0};
	WNDCLASSEX actionMenu={0};
	actionMenu.cbSize=sizeof(WNDCLASSEX);
	actionMenu.lpfnWndProc=MenuProc;
	actionMenu.lpszClassName=MENU_WINDOW_CLASS;
	actionMenu.hInstance=h1;
	actionMenu.hCursor=idc_arrow;
	wc.cbSize=sizeof(wc);
	wc.lpfnWndProc=WndProc;
	wc.lpszClassName="WindowClass";
	wc.hInstance=h1;
	wc.hCursor=idc_arrow;
	wc.hIcon=ic_logo;
	wc.hIconSm=ic_logo;
	/*WNDCLASSEX pageclass={0};
	pageclass.cbSize=sizeof(wc);
	pageclass.lpfnWndProc=PageProc;
	pageclass.lpszClassName="PageClass";
	pageclass.hInstance=h1;
	pageclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	pageclass.style=CS_VREDRAW|CS_HREDRAW;*/
	gsi.GdiplusVersion=1;
	gsi.DebugEventCallback=NULL;
	gsi.SuppressBackgroundThread=FALSE;
	gsi.SuppressExternalCodecs=FALSE;
	GdiplusStartup(&gdipToken,&gsi,NULL);
	HRSRC hrsrc=FindResourceA(NULL,"IC_MENU",RT_RCDATA);
	HGLOBAL hglobal=LoadResource(NULL,hrsrc);
	LPVOID lpvoid=LockResource(hglobal);
	DWORD size=SizeofResource(h1,hrsrc);
	HGLOBAL hg=GlobalAlloc(GMEM_MOVEABLE,size);
	void* buffer=GlobalLock(hg);
	CopyMemory(buffer,lpvoid,size);
	GlobalUnlock(buffer);
	IStream *is;
	HRESULT hResult=CreateStreamOnHGlobal(hg,TRUE,&is);
	GdipCreateBitmapFromStream(is,&ic_menu);
	GlobalFree(hg);
	is->lpVtbl->Release(is);
	//HANDLE outfile=CreateFileA("./out.png",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	//WriteFile(outfile,lpvoid,size,0,NULL);
	if(!RegisterClassEx(&wc)){
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);return 0;
	}
	if(!RegisterClassEx(&actionMenu)){
		MessageBox(NULL,"Window Class Registration Failed","",0);return 0;
	}
	/*if(!RegisterClassEx(&pageclass)){
		MessageBox(NULL,"Can not register window class:PageClass","Error",0);return 0;
	}*/
	int mainw=640,mainh=480;
	gui = CreateWindowExA(0,"WindowClass","dev",WS_POPUP,CW_USEDEFAULT,CW_USEDEFAULT,mainw,mainh,NULL,NULL,h1,NULL);
	if(gui== NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}
	actionMenuWindow=CreateWindowExA(0,MENU_WINDOW_CLASS,"dev.menu",WS_POPUP,0,0,mainw*2/3,mainh,gui,NULL,h1,NULL);
	//int left
	//page=CreateWindowEx(0,"PageClass","",WS_POPUP,CW_USEDEFAULT
	ShowWindow(gui,num);//SW_SHOW);
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0)) { 
		TranslateMessage(&msg); 
		DispatchMessage(&msg);
	}
	GdiplusShutdown(gdipToken);
	return msg.wParam;
}